package com.example.administrator.androidros.activity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.androidros.R;
import com.example.administrator.androidros.base.BaseActivity;
import com.example.administrator.androidros.base.BaseResponseResultModel;
import com.example.administrator.androidros.net.HttpUtils;
import com.example.administrator.androidros.utils.AgentConstants;
import com.example.administrator.androidros.utils.AgentUtils;
import com.example.administrator.androidros.utils.MD5;
import com.example.administrator.androidros.utils.MobSMSUtil;
import com.example.administrator.androidros.utils.ServerConstants;
import com.example.administrator.androidros.utils.StringUtils;
import com.example.administrator.androidros.widget.CustomCountDown;
import com.example.administrator.androidros.widget.CustomTipDialog;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class FindPasswordActivity extends BaseActivity {
    private static final String TAG = "hhc";
    @BindView(R.id.activity_find_password_edit_phone)
    protected EditText activity_find_password_edit_phone;

    @BindView(R.id.activity_find_password_edit_captcha)
    protected EditText activity_find_password_edit_captcha;

    @BindView(R.id.activity_find_password_bt_captcha)
    protected Button activity_find_password_bt_captcha;

    @BindView(R.id.activity_find_password_edit_password)
    protected EditText activity_find_password_edit_password;

    @BindView(R.id.activity_find_password_edit_password_confirm)
    protected EditText activity_find_password_edit_password_confirm;

    private CustomCountDown customCountDown;
    private MobSMSUtil mobSMSUtil;
    private String memberTelephone;
    private String memberPassword_confirm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_find_password);
    }

    @Override
    protected void initData() {
        initCommonHeader();
        initMobSMS();
    }

    @Override
    protected void onFailure(int requestCode, Object object) {
        showToast(getString(R.string.common_response_failure));
        stopTipDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        if (object != null) {
            switch (requestCode) {
                case 1001:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                    } else if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_FAIL)) {
                        mobSMSUtil.getVerificationCode(memberTelephone);
                    }
                    break;
                case 1002:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                    } else if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_FAIL)) {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
            }
        }
    }

    protected void initCommonHeader() {
        configCommonHeader(this, getString(R.string.find_password_title), false);
    }

    protected void initMobSMS() {
        mobSMSUtil = new MobSMSUtil(mBaseActivity, new MobSMSUtil.MobResultCallBack() {
            @Override
            public void onSendSuccess() {
                startTipDialog(CustomTipDialog.ICON_TYPE_SUCCESS, getString(R.string.toast_send_success), false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        stopTipDialog();
                    }
                }, 1500);

                customCountDown = new CustomCountDown(60000, 1000, activity_find_password_bt_captcha);
                customCountDown.start();
            }

            @Override
            public void onValidateSuccess() {
                doUpdate();
            }

            @Override
            public void onValidateFailed() {
                showToast(getString(R.string.toast_captcha_error));
            }
        });
    }

    @OnClick(R.id.activity_find_password_bt_captcha)
    protected void getVerificationCode() {
        this.memberTelephone = activity_find_password_edit_phone.getText().toString();
        if (memberTelephone.indexOf(" ") != -1) {
            memberTelephone = memberTelephone.replace(" ", "");
        }
        if (!AgentUtils.isMobile(memberTelephone)) {
            showToast(getString(R.string.toast_telephone_format_warn));
            return;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberTelephone", memberTelephone);
        jsonObject.put("type", "10");
        HttpUtils.post(this, 1001, ServerConstants.REGISTER, jsonObject.toString(), this);
    }

    @OnClick(R.id.activity_find_password_bt_confirm)
    protected void updateClick() {
        this.memberTelephone = activity_find_password_edit_phone.getText().toString();
        this.memberPassword_confirm = activity_find_password_edit_password_confirm.getText().toString();
        String captcha = activity_find_password_edit_captcha.getText().toString();
        String memberPassword = activity_find_password_edit_password.getText().toString();

        if (memberTelephone.indexOf(" ") != -1) {
            memberTelephone = memberTelephone.replace(" ", "");
        }
        if (StringUtils.isBlank(memberTelephone)) {
            showToast(R.string.toast_telephone_null);
            return;
        }
        if (!AgentUtils.isMobile(memberTelephone)) {
            showToast(R.string.toast_telephone_format_warn);
            return;
        }
        if (StringUtils.isBlank(captcha)) {
            showToast(R.string.toast_captcha_null);
            return;
        }

        if (captcha.length() < 6) {
            showToast(R.string.toast_captcha_length_error);
            return;
        }

        if (StringUtils.isBlank(memberPassword)) {
            showToast(R.string.toast_password_null);
            return;
        }

        if (memberPassword.length() < 6) {
            showToast(R.string.toast_password_length_error);
            return;
        }

        if (StringUtils.containSpace(memberPassword_confirm)) {
            showToast(R.string.toast_password_space);
            return;
        }
        if (StringUtils.isBlank(memberPassword_confirm)) {
            showToast(R.string.toast_password_confirm_null);
            return;
        }
        if (!memberPassword_confirm.equals(memberPassword)) {
            showToast(R.string.toast_password_confirm_warn);
            return;
        }
        mobSMSUtil.validate(memberTelephone, captcha);
    }

    private void doUpdate() {
        String MD5Password = MD5.encryptToMD5(memberPassword_confirm);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberTelephone", memberTelephone);
        jsonObject.put("memberPassword", MD5Password);
        jsonObject.put("type", "30");
        HttpUtils.post(this, 1002, ServerConstants.REGISTER, jsonObject.toString(), this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mobSMSUtil.destroy();
    }


    @Override
    protected void onStart() {
        super.onStart();
        Logger.e("---onStart---");
    }

    @Override
    public void onResume() {
        super.onResume();
        Logger.e("onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Logger.e("onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Logger.e("onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Logger.e("onRestart");
    }
}
